import { list, detail, update, create, remove } from '../services/app';
import { users } from 'src/services/frame';
import { stateCreate, effectsCreate, reducers, dataHandle } from 'src/models/modelCreator';

let localDataHandle = {
  ...dataHandle,
  * usersHandle({ data, error }, { put }) {
    if (data) {
      yield put({
        type: 'loadUsersSuccess',
        payload: { users: data.datas },
      });
    } else if (error && error.fieldErrors) {
      yield put({
        type: 'putError',
        payload: {
          fieldErrors: error.fieldErrors,
        }
      });
    }
  },
};

export default {
    namespace: 'app',
    state: {
      ...stateCreate(),
      moduleName: 'app',
      entityDescription: '项目',
      searchParams: {},
    },
    effects: {
      ...effectsCreate({ list, detail, update, create, remove }, localDataHandle),

      * list({ payload, }, { call, put, select }) {
        const searchParams = yield select(state => state[payload.module]['searchParams']);
        const { data, error } = yield call(list, searchParams);
        yield dataHandle.nakeListHandle({ data, error }, { put });
      },
    },
    reducers: {
      ...reducers,
    },
}
